package com.zhangyu.shadow.repair.delegate.search;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.zhangyu.shadow.repair.R;
import com.zhangyu.shadow.repair.R2;
import com.zhangyu.shadow.repair.delegate.bean.BusBean;
import com.zhangyu.shadow.repair.event.ViewPagerClickEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import butterknife.BindView;
import st.lib.base.delegate.BaseDelegate;

/**
 * @author zhangyu
 * @date 2018/11/2
 */

public class TabFragment extends BaseDelegate {
    public static TabFragment newInstance(ArrayList<BusBean> busBeans) {
        Bundle arguments = new Bundle();
        arguments.putSerializable("list", busBeans);
        TabFragment fragment = new TabFragment();
        fragment.setArguments(arguments);
        return fragment;
    }

    @BindView(R2.id.recycler_view)
    RecyclerView mRecyclerView;

    private ArrayList<BusBean> mBusBeans;
    private BusListAdapter mBusListAdapter;

    @SuppressWarnings("unchecked")
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mBusBeans = (ArrayList<BusBean>) bundle.getSerializable("list");
        }
    }

    @Override
    public Object setLayout() {
        return R.layout.view_maintain_tab;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mBusListAdapter = new BusListAdapter(getContext(),
                R.layout.item_maintain_bus_info, mBusBeans);
        mBusListAdapter.setEmptyView(R.layout.view_list_empty, (ViewGroup) mRecyclerView.getParent());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.setAdapter(mBusListAdapter);
        mRecyclerView.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (!isFastClick()) {
                    BusBean busBean = (BusBean) adapter.getItem(position);
                    if (busBean != null) {
                        EventBus.getDefault().
                                post(ViewPagerClickEvent.getInstance().setBusBean(busBean));
                    }
                }
            }
        });
    }

    public void dataChanged(ArrayList<BusBean> busBeans) {
        if (mBusBeans == null) {
            mBusBeans = new ArrayList<>();
        }
        mBusBeans.clear();
        mBusBeans.addAll(busBeans);
        if (mBusListAdapter != null) {
            mBusListAdapter.notifyDataSetChanged();
        }
    }

}
